package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/checkitem")
public class CheckItemController {
    @Reference
    CheckItemService checkItemService;
    @Autowired
    private JedisPool jedisPool;

    @PreAuthorize("hasAnyAuthority('CHECKITEM_ADD')")
    @RequestMapping("/add")
    //添加
    public Result add(@RequestBody CheckItem checkItem) {

        try {
            //调用service方法
            checkItemService.add(checkItem);
        } catch (Exception e) {
            //失败返回Result对象
            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
        }
        //成功返回Result对象
        return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
    }

    @PreAuthorize("hasAnyAuthority('CHECKITEM_QUERY')")
    @RequestMapping("/findPage")
    //分页查询 方法内使用QueryPageBean 返回PageResult
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        //调用service方法返回PageResult
        PageResult pageResult = checkItemService.pageQuery(
                //queryPageBean get当前页码
                queryPageBean.getCurrentPage(),
                //queryPageBean get每页显示的记录数
                queryPageBean.getPageSize(),
                //queryPageBean get查询条件
                queryPageBean.getQueryString());
        //返回pageResult
        return pageResult;
    }

    @PreAuthorize("hasAnyAuthority('CHECKITEM_DELETE')")
    @RequestMapping("/delete")
    //删除
    public Result delete(Integer id) {
        //try
        try {
            //调用service方法
            checkItemService.delete(id);
        } catch (RuntimeException e) {
            //返回异常错误
            return new Result(false, e.getMessage());
        } catch (Exception e) {
            //返回删除失败
            return new Result(false, MessageConstant.DELETE_CHECKITEM_FAIL);
        }
        //返回删除成功
        return new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
    }

    @PreAuthorize("hasAnyAuthority('CHECKITEM_EDIT')")
    @RequestMapping("/edit")
    //编辑
    public Result edit(@RequestBody CheckItem checkItem) {
        //try
        try {
            //调用service方法
            checkItemService.edit(checkItem);
        } catch (Exception e) {
            //返回编辑失败
            return new Result(false, MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
        //返回编辑成功
        return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
    }


    @RequestMapping("/findById")
    //根据id查询item信息
    public Result findById(Integer id) {
        //try
        try {
            //调用service方法，返回checkItem
            CheckItem checkItem = checkItemService.findById(id);
            //返回查询成功
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, checkItem);
        } catch (Exception e) {
            e.printStackTrace();
            //返回查询失败
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    @RequestMapping("/findAll")
    //查询所有
    public Result findAll() {
        //调用service返回checkItem list集合
        List<CheckItem> checkItemList = checkItemService.findAll();
        if (checkItemList != null && checkItemList.size() > 0) {
            Result result = new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS);
            result.setData(checkItemList);
            return result;
        } else {
            return new Result(false, MessageConstant.QUERY_CHECKITEM_SUCCESS);
        }
    }


}
